package com.example.cinemasystemv2.repository;

import com.example.cinemasystemv2.entity.Ticket;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface TicketRepository extends JpaRepository<Ticket, Long> {
    
    Optional<Ticket> findByTicketNumber(String ticketNumber);
    
    List<Ticket> findByOrderId(String orderId);
    
    List<Ticket> findByScreeningId(Long screeningId);
    
    List<Ticket> findByCreatedAtBetween(LocalDateTime startTime, LocalDateTime endTime);
    
    boolean existsByTicketNumber(String ticketNumber);
    
    long countByScreeningId(Long screeningId);
    
    @Query("SELECT t.seatNumber FROM Ticket t WHERE t.screeningId = :screeningId")
    List<String> findSeatNumbersByScreeningId(@Param("screeningId") Long screeningId);
    
    @Query("SELECT t FROM Ticket t WHERE t.isChildTicket = :isChildTicket")
    List<Ticket> findByTicketType(@Param("isChildTicket") boolean isChildTicket);
    
    @Query("SELECT SUM(t.price) FROM Ticket t WHERE t.screeningId = :screeningId")
    Double getTotalSalesByScreeningId(@Param("screeningId") Long screeningId);
    
    @Query("SELECT SUM(t.price) FROM Ticket t WHERE t.createdAt BETWEEN :startTime AND :endTime")
    Double getTotalSalesByDateRange(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);
}